package com.le.buddha.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import com.alibaba.fastjson.JSONObject;
import com.le.buddha.common.Response;
import com.le.buddha.mobile.Good;
import com.le.buddha.mobile.Order;
import com.le.buddha.mapper.GOrderMapper;
import com.le.buddha.mobile.User;
import com.le.buddha.mobile.bo.WxOrderBO;
import com.le.buddha.service.IGoodService;
import com.le.buddha.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.le.buddha.service.IUserService;
import com.le.buddha.service.IWxPayService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author guole
 * @since 2024-10-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<GOrderMapper, Order> implements IOrderService {
    @Resource
    private IGoodService goodService;
    @Resource
    private IUserService userService;
    @Resource
    private IWxPayService wxPayService;
    @Override
    public JSONObject createOrder(Integer goodId, Long userId) {
        //向微信发起订单创建
        //创建自己的订单数据
        Good good = goodService.getById(goodId);
        User user = userService.getById(userId);
        Order order = new Order();
        order.setGoodId(good.getId());
        order.setUserId(user.getId());
        order.setAmount(good.getCurrentPrice());
        order.setNum(1);
        order.setCreateTime(new Date());
        order.setOutTradeNo(UUID.fastUUID().toString().replace("-", ""));
        order.setDescription("购买" + good.getName());
        WxOrderBO wxOrderBO = BeanUtil.toBean(order, WxOrderBO.class);
        JSONObject wxJson = wxPayService.createOrder(wxOrderBO);
        order.setPayOrderJson(wxJson.toJSONString());
        this.save(order);
        return wxJson;
    }
}
